import  './401'
import $ from './jquery.js'


    $(function () {

        let filename;
        let user;

        $.ajax({
            url: "http://192.168.30.63:3000/users/getUserInfo",
            type: "get",
            success(res) {
                if (res.code == 200) {
                    $("#password").val(res.user.password)
                    $(".yh").attr("src",`http://192.168.30.63:3000/images/${res.user.head}`)
                    
                    user = res.user
                }
            }

        })



        $("#upload").change(function () {
            //获得被选中的文件数组
            let files = this.files;


            console.log(files[0]);
            //创建formData对象,(ajax发送的参数)
            let fd = new FormData();
            //把文件添加到formData对象
            //参数1：对应后端的file，做一个匹配
            //参数2：需要上传的文件对象
            fd.append("file", files[0])
            //发送ajax
            $.ajax({
                url: "http://192.168.30.63:3000/users/upload",
                //必须用post提交
                type: "post",
                //提交参数
                data: fd,
                //让ajax不用处理传递的文件内容
                processData: false,
                contentType: false,
                success(res) {

                    console.log(1, res)
                    $("img").attr("src", "http://192.168.30.63:3000/temp/" + res.files.filename);
                    filename = res.files.filename
                }
            })

        })


        $("#btn").click(function () {
            console.log("filename", filename)
            $.ajax({
                url: "http://192.168.30.63:3000/users/upload_sure",
                type: "post",
                data: {
                    password: $("#password").val(),
                    filename,
                    _id: user._id
                },
                success(res) {
                    if (res.code == 200) {

                        alert("修改成功")
                        location.href="./login.html"
                    }
                }

            })
        })
    })